package cate.game.play.skill.passive.hero;

import cate.common.table.d.GDFight;
import cate.common.util.XT;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.proce.action.ctx.ActionCtx;
import cate.game.play.proce.action.ctx.TimeActionCtx;
import cate.game.play.proce.move.AppendMove;
import cate.game.play.proce.move.AppendMoveHandler;
import cate.game.play.proce.move.MoveCtx;
import cate.game.play.proce.turn.TurnCtx;
import cate.game.play.skill.Skill;
import cate.game.play.skill.passive.PassiveHandler;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

/**
 * 己方英雄出手时，发动一次系数为106%的普通攻击(每个回合最多6次，每个队友出手最多触发2次）
 */
public class 审判恩赐PH extends PassiveHandler implements AppendMoveHandler {


	private double 附加系数;

	private int 回合次数;

	private int 队友次数;

	//附加系数=600&回合次数=6&队友次数=2
	@Override
	public void parse(String code) {
		PlayCfgArgs args =new PlayCfgArgs(code);
		附加系数 = args.getDouble("附加系数", 0d);
		回合次数 = args.getInt("回合次数", 0);
		队友次数 = args.getInt("队友次数", 0);
	}


	@Override
	public void onRoundBegin(ActionCtx action) {
		pidNum.clear();
	}

	private Map<Integer,Integer> pidNum = new HashMap<>();

	private int totalNum() {
		return pidNum.values().stream().mapToInt(a -> a).sum();
	}

	private boolean allowAppend(int targetPid){
		if (pidNum.getOrDefault(targetPid, 0) >= 队友次数) {
			return false;
		}
		if (totalNum() >= 回合次数) {
			return false;
		}
		return true;
	}

	private LinkedList<Integer> pids = new LinkedList<>();

	@Override
	public void onMoveDone(MoveCtx move, TimeActionCtx action) {
		if(skill.owner.isDead()){
			return;
		}
		if (move.getActor().getTeam() != skill.owner.getTeam()) {
			return;
		}
		if (move.getActor().type != GDFight.Fighter.HERO) {
			return;
		}
		if (move.getActor().getPid() == skill.owner.getPid()) {
			return;
		}
		if(!allowAppend(move.getActor().getPid())){
			return;
		}
		Skill attack = skill.owner.skill.getAttack();
		if (attack == null) {
			return;
		}
		attack.param.addHpCfgByCoeff(附加系数);
		move.getTurn().addAppend(new AppendMove(skill.owner, attack, this));
		pids.add(move.getActor().getPid());
	}

	@Override
	public boolean inValid(TurnCtx turn, AppendMove appendMove) {
		if (XT.isBlank(pids)) {
			return true;
		}
		Integer pid = pids.removeFirst();
		if (!allowAppend(pid)) {
			return true;
		}
		pidNum.put(pid, pidNum.getOrDefault(pid, 0) + 1);
		return false;
	}

	@Override
	public void beforeAppendMove(TurnCtx turn, AppendMove appendMove) {

	}

	@Override
	public void afterAppendMove(TurnCtx turn, AppendMove appendMove) {

	}

	@Override
	public void onAppendMoveFail(TurnCtx turn, AppendMove appendMove) {

	}
}
